package com.dongdongshop.shiro;

import com.dongdongshop.pojo.TbAdmin;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.AdminService;
import com.dongdongshop.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;


@Component
public class LoginRealm extends AuthorizingRealm {

    @DubboReference
    private UserService userService;


    //授权的方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        TPerson person = (TPerson) SecurityUtils.getSubject().getPrincipal();
//        List<String> perms = menuService.findMenuPermsByPersonId(person.getPid());
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.addStringPermissions(perms);
        return null;
    }

    //认证的方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token =  (UsernamePasswordToken)authenticationToken;
        TbUser user = userService.getLogin(token.getUsername());
        if (user == null){
            return null;
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(),ByteSource.Util.bytes(user.getSalt()),user.getUsername());
    }
}
